<?php
header("content-type","text/xml;charset=utf-8");
include("/var/www/html/new/d/module/api/HuaQiaoCheng/des.class.php");
/**
**华侨城对接
**2015-11-24 am 10
**User by PZP
**测试数据:	接口测试服务地址：http://cs.mopon.cn:8126/ota/httpApi/xml
			预付：100100001320  到付：100100001321    渠道id：distri735  密钥：B53NR6YE
**/
class HuaQiaoCheng{
	private $DistributorId = 'distri735';
	private $secretKey = 'B53NR6YE';
	private $ReUrl = 'http://cs.mopon.cn:8126/ota/httpApi/xml';
	
	function __construct(){
		$this->hqc_des = new Crypt3Des($this->secretKey);
	}
	
	//获取景区
	public function Get_Scenic(){
		$TransactionCode = "01" ; //交易码
		$SequenceId = 'pft2015112501';		
		$body = '<?xml version="1.0" encoding="UTF-8"?><Body><ScenicId></ScenicId><ScenicName></ScenicName></Body>';
		$sign = base64_encode(md5($SequenceId.$this->DistributorId.strlen($body)));
		$body_des = $this->hqc_des->encrypt($body);
		$requestData  = $this->hqc_header($TransactionCode,$SequenceId,$body_des,$sign);
		$hqc_postData = $this->hqc_post($requestData);
		$hqc_postData_body = simplexml_load_string($this->hqc_des->decrypt((string)$hqc_postData->Body));
		print_r($hqc_postData_body);
	}
	
	//头部
	public function hqc_header($TransactionCode,$SequenceId,$body_des,$sign){
		$time = date('Y-m-d H:i:s');
		$header = '<?xml version="1.0" encoding="UTF-8"?><OTM><Head><Version>2.1.0</Version><SequenceId>'.$SequenceId.'</SequenceId><TimeStamp>'.$time.'</TimeStamp><DistributorId>'.$this->DistributorId.'</DistributorId><TransactionCode>'.$TransactionCode.'</TransactionCode><Signed>'.$sign.'</Signed></Head><Body>'.$body_des.'</Body></OTM>';
		return $header;
	}
	
	public function  hqc_post($hqc_postData)
    {
		$array = array();
		$array['xmlContent'] = $hqc_postData;
		$postData = http_build_query($array);
				// print_r($array);exit;
		$header = array("Content-Type"=>"application/x-www-form-urlencoded");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->ReUrl);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $res = curl_exec($ch);
        curl_close($ch);
		$res = simplexml_load_string( $res, 'SimpleXMLElement', LIBXML_NOCDATA );
        return $res;
    }
	
}

$hqc = new HuaQiaoCheng();
$Get_Scenic = $hqc->Get_Scenic();
eNum><AppointTripDate>2015-11-27</AppointTripDate></Body>';
		$response = $this->PackageData($TransactionCode,$body,$SequenceId);
		return $response;
	}
	
	//订单退款接口
	public function ModifyOrder(){
		$TransactionCode = "09" ; //交易码
		$SequenceId = date('Y-m-d').'09';	
		$body = '<?xml version="1.0" encoding="UTF-8"?><Body><OutOrderId>pft2015112701001</OutOrderId><OtmOrderId>201511270473522D</OtmOrderId><VoucherValue>79011188924209</VoucherValue><RefundQuantity>1</RefundQuantity></Body>';
		$response = $this->PackageData($TransactionCode,$body,$SequenceId);
		return $response;
	}
	
	//订单详情查询接口(预付订单查询)
	public function QueryOrder(){
		$TransactionCode = "05" ; //交易码
		$SequenceId = date('Y-m-d').'05';	
		$body = '<?xml version="1.0" encoding="UTF-8"?><Body><OutOrderId>pft2015112601001</OutOrderId><OtmOrderId>201511260473482D</OtmOrderId></Body>';
		$response = $this->PackageData($TransactionCode,$body,$SequenceId);
		return $response;
	}
	
	//订单详情查询接口(到付订单查询)
	public function ToPayQueryOrder(){
		$TransactionCode = "06" ; //交易码
		$SequenceId = date('Y-m-d').'06';	
		$body = '<?xml version="1.0" encoding="UTF-8"?><Body><OutOrderId>pft2015112601007</OutOrderId><OtmOrderId>201511270000208D-TPA</OtmOrderId></Body>';
		$response = $this->PackageData($TransactionCode,$body,$SequenceId);
		return $response;
	}
	
	//凭证重发接口
	public function ReSendSMS(){
		$TransactionCode = "08" ; //交易码
		$SequenceId = date('Y-m-d').'08';	
		$body = '<?xml version="1.0" encoding="UTF-8"?><Body><OutOrderId>pft2015112601001</OutOrderId><OtmOrderId>201511260473482D</OtmOrderId><VoucherId>2015112600889671</VoucherId><VoucherValue>79077722561179</VoucherValue></Body>';
		$response = $this->PackageData($TransactionCode,$body,$SequenceId);
		return $response;
	}
	
	//封装数据及提交数据 
	public function PackageData($TransactionCode,$body,$SequenceId,$name_length=NULL){
		$sign = base64_encode(md5($SequenceId.$this->DistributorId.mb_strlen($body,'UTF-8')));
		$body_des = $this->hqc_des->encrypt($body);
		$requestData  = $this->hqc_header($TransactionCode,$SequenceId,$body_des,$sign);
		$hqc_postData = $this->hqc_post($requestData);
		// print_r($requestData);exit;
		if((int)$hqc_postData->Head->StatusCode == 200 &&  (string)$hqc_postData->Head->Message == 'SUCCESS'){
			$hqc_postData_body = simplexml_load_string($this->hqc_des->decrypt((string)$hqc_postData->Body));
			return $hqc_postData_body;
		}else{
			return (int)$hqc_postData->Head->StatusCode.'|'.(string)$hqc_postData->Head->Message;
		}
	}
	
	
	//头部
	public function hqc_header($TransactionCode,$SequenceId,$body_des,$sign){
		$time = date('Y-m-d H:i:s');
		$header = '<?xml version="1.0" encoding="UTF-8"?><OTM><Head><Version>2.1.0</Version><SequenceId>'.$SequenceId.'</SequenceId><TimeStamp>'.$time.'</TimeStamp><DistributorId>'.$this->DistributorId.'</DistributorId><TransactionCode>'.$TransactionCode.'</TransactionCode><Signed>'.$sign.'</Signed></Head><Body>'.$body_des.'</Body></OTM>';
		return $header;
	}
	
	
	//post提交
	public function  hqc_post($hqc_postData)
    {
		$array = array();
		$array['xmlContent'] = $hqc_postData;
		$postData = http_build_query($array);
		$header = array("Content-Type"=>"application/x-www-form-urlencoded");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->ReUrl);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $res = curl_exec($ch);
        curl_close($ch);
		$res = simplexml_load_string( $res, 'SimpleXMLElement', LIBXML_NOCDATA );
        return $res;
    }
	
	//日志记录
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/HuaQiaoCheng/'.$path.'.txt';
		$word = date("Y-m-d H:i:s")."|".$word;
		if(file_exists($filepath)){
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
		}
		$fp = fopen($filepath,"a");
		flock($fp, LOCK_EX);
		fwrite($fp,$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
} 
$hqc = new HuaQiaoCheng();
// $Get_Scenic = $hqc->Get_Scenic();print_r($Get_Scenic);
// $Get_Scenic_Details = $hqc->Get_Scenic_Details(); print_r($Get_Scenic_Details);
// $CheckGoods = $hqc->CheckGoods(); print_r($CheckGoods);
$PayedOrder = $hqc->PayedOrder(); print_r($PayedOrder);
// $ToPayOrder = $hqc->ToPayOrder(); print_r($ToPayOrder);
// $QueryOrder = $hqc->QueryOrder(); print_r($QueryOrder);
// $ToPayQueryOrder = $hqc->ToPayQueryOrder(); print_r($ToPayQueryOrder);
// $ReSendSMS = $hqc->ReSendSMS(); print_r($ReSendSMS);
// $ModifyOrder = $hqc->ModifyOrder(); print_r($ModifyOrder);
